//
//  LzwIndexDetailVc.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/5.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwIndexDetailVc.h"
#import "LzwIndexDetailCell.h"
#import "LzwBangumiDetailVC.h"

@interface LzwIndexDetailVc ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myDetailTableView;

@end

@implementation LzwIndexDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"详情页";
    self.view.backgroundColor = [UIColor whiteColor];

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setImage:[[UIImage imageNamed:@"L20.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(LeftButtonAxtion:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
    
    //初始化数组
    self.arraydetail = [NSMutableArray array];
    
    
    //创建tableview
    [self creatTableViewDetail];
    
}

- (void)creatTableViewDetail{
    
    self.myDetailTableView = ({
    
        UITableView *myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
        myTableView.backgroundColor = [UIColor whiteColor];
        myTableView.delegate = self;
        myTableView.dataSource =self;
        [self.view addSubview:myTableView];
    
        myTableView;
    });
    self.myDetailTableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    [self.myDetailTableView registerClass:[LzwIndexDetailCell class] forCellReuseIdentifier:@"cell"];
    
    [GiFHUD show];
    [GiFHUD setGifWithImageName:@"hold.gif"];
    
    //请求数据
    [self getDetailData:_tagId];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arraydetail.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwIndexDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.modleDetail = _arraydetail[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 150;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    LzwBangumiDetailVC *bangVc = [[LzwBangumiDetailVC alloc]init];
    
    LzwIndexDetailModle *model = _arraydetail[indexPath.row];
    
    bangVc.ting_ID = model.season_id.intValue;
    
    [self.navigationController pushViewController:bangVc animated:YES];
    
    
}

- (void)getDetailData:(NSString *)ID{
    
    NSString *str = [NSString stringWithFormat:@"http://bangumi.bilibili.com/api/get_season_by_tag_v2?_device=android&_hwid=53c9b4a7dd017094&appkey=c1b107428d337928&build=414000&page=1&pagesize=20&platform=android&tag_id=%@&ts=1459841105000&sign=892ee6a518c5362ffb3761e8a11a3af7 HTTP/1.1", ID];
  
    [WZAFNetTool GetNetWithURL:str WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        NSMutableDictionary *dic = [reuslt objectForKey:@"result"];
        
        NSMutableArray *array = [dic objectForKey:@"list"];
        
        for (NSMutableDictionary *dict in array) {
            
            LzwIndexDetailModle *modelDetail = [[LzwIndexDetailModle alloc]init];
            
            [modelDetail setValuesForKeysWithDictionary:dict];
            
            [self.arraydetail addObject:modelDetail];
            
        }
        
        [self.myDetailTableView reloadData];
        
        [GiFHUD dismiss];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}


- (void)LeftButtonAxtion:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
